<?php
	class Dharmaksatrias extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->model('dharmaksatria');
			$this->load->helper('form');
			$this->id_user = $this->session->userdata('id_user');
			$this->name = $this->session->userdata('name');
		}
		
		public function index(){
			$this->validation();
			$data = array(
				'header' => 'Dharmaksatria Halaman Admin',
				'breadcrumbs' => 'none',
			);
			$this->load->view('part/header-admin', $data);
			$this->load->view('dharmaksatria/index');
			$this->load->view('part/footer-admin');
		}
		
		public function validation(){
			if(! $this->id_user){
				redirect('dharmaksatrias/login');
			}
		}
		
		public function login(){
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			
			if ($username || $password) {
				if ($user = $this->dharmaksatria->login($username, $password)) {
					$this->session->set_userdata(
						array(
							'id_user' => $user['id_user'], //untuk menyimpan id admin yang login ke session
							'name' => $user['username'], //untuk menyimpan nama dari admin ke session
						)
					);
				} else {
					$this->session->set_flashdata('login_msg', 'Incorrect username and/or password');
				}
				redirect('dharmaksatrias');
			} else {
				$this->load->view('dharmaksatria/login.php');
			}
		}
		
		public function logout(){
			$this->session->sess_destroy(); 
			redirect('dharmaksatrias');
		}
		
		public function user($page = 'index', $id = null, $type = null){
			$this->validation();
			$this->load->model('user');
			switch ($page) {
				case 'add':
					$header = array(
						'header' => 'Tambah User',
						'breadcrumbs' => 'anggota',
					);
					if(isset($_POST['add_user'])){
						$post_user_data = array(
							'username' => $this->input->post('username'),
							'password' => md5($this->input->post('password')),
							'aktif' => $this->input->post('aktif'),
							'level' => $this->input->post('level'),
						);
						$id = $this->user->add($post_user_data);
						if($post_user_data['level'] == ALUMNI_TYPE){
							$post_alumni_data = array(
								'id_user' => $id,
								'nama' => $this->input->post('nama'),
								'jk' => $this->input->post('jk'),
								'pekerjaan' => $this->input->post('pekerjaan'),
								'alamat' => $this->input->post('alamat'),
								'telepon' => $this->input->post('telepon'),
								'email' => $this->input->post('email'),
								'hp' => $this->input->post('hp'),
								'status' => $this->input->post('status'),
							);
							$this->user->add_alumni($post_alumni_data);
							copy("./images/user_avatars/default.jpg", "./images/user_avatars/" . $id . ".jpg");
						}
						redirect('dharmaksatrias/user');
					}
					$this->load->view('part/header-admin', $header);
					$this->load->view('dharmaksatria/user/add');
					$this->load->view('part/footer-admin');
					break;
					
				case 'edit':
					if ($id) {
						$header = array(
							'header' => 'Edit User',
							'breadcrumbs' => 'anggota',
						);
						$data['user'] = $this->user->get_alumni($id);
						if(isset($_POST['edit_user'])){
							$post_user_data = array(
								'username' => $this->input->post('username'),
								'password' => md5($this->input->post('password')),
								'aktif' => $this->input->post('aktif'),
								'level' => $this->input->post('level'),
							);
							$this->user->edit($id, $post_user_data);
							if($post_user_data['level'] == ALUMNI_TYPE){
								$post_alumni_data = array(
									'nama' => $this->input->post('nama'),
									'jk' => $this->input->post('jk'),
									'pekerjaan' => $this->input->post('pekerjaan'),
									'alamat' => $this->input->post('alamat'),
									'telepon' => $this->input->post('telepon'),
									'email' => $this->input->post('email'),
									'hp' => $this->input->post('hp'),
									'status' => $this->input->post('status'),
								);
								$this->user->edit_alumni($id, $post_alumni_data);
							}
							redirect('dharmaksatrias/user');
						}
						$this->load->view('part/header-admin', $header);
						$this->load->view('dharmaksatria/user/edit', $data);
						$this->load->view('part/footer-admin');
					} else {
						$this->load->view('dharmaksatria/wrong-room');
					}
					break;
					
				case 'delete':
					$data = $this->dharmaksatria->get('alumni', 'foto', 'id_user = '.$id);
					$this->dharmaksatria->delete('user', 'id_user', $id);
					if($type == ALUMNI_TYPE){
						$this->dharmaksatria->delete('alumni', 'id_user', $id);
						if($data[0]['foto'] != 'default.jpg'){
							unlink('./images/user_avatars/'.$data[0]['foto']);
						}
						
					}
					redirect('dharmaksatrias/user');
					break;
				
				case 'activate':
					$this->dharmaksatria->edit('id_user', $id, 'user', array('aktif' => ACTIVE_ACCOUNT));
					redirect('dharmaksatrias/user');
					break;
				
				case 'index':
				default:
					$data = array(
						'header' => 'Data User',
						'breadcrumbs' => 'anggota',
					);
					$this->load->view('part/header-admin', $data);
					$data = array(
						'users' => $this->dharmaksatria->get('user', '*', 'level = 2 order by id_user desc'),
					);
					$this->load->view('dharmaksatria/user/index', $data);
					$this->load->view('part/footer-admin');
					break;
			}
		}

		public function artikel($page = 'index', $id = null, $type = null){
			$this->validation();
			$this->load->model('article');
			switch ($page) {
				case 'add':
					$header = array(
						'header' => 'Tambah Artikel',
						'breadcrumbs' => 'artikel',
					);
					if(isset($_POST['add_artikel'])){
						$random           = rand(000000,999999);
						$image_upload 	  = $_FILES['image_article']['name'];
						if (empty($image_upload)) {
							copy("./images/article_images/default.jpg", "./images/article_images/" . $random . "_default.jpg");
							$article_image_name = $random."_default.jpg";
						}else{
							$article_image_name = $random."_".strtolower($image_upload);
						}
						$post_artikel_data = array(
							'id_user' => $this->id_user,
							'judul' => $this->input->post('judul'),
							'kategori' => $this->input->post('kategori'),
							'isi' => $this->input->post('isi'),
							'status' => $this->input->post('status'),
							'foto' => $article_image_name,
						);
						$this->article->add($post_artikel_data);
						copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
						redirect('dharmaksatrias/user/index');
					}
					$this->load->view('part/header-admin', $header);
					$this->load->view('dharmaksatria/artikel/add');
					$this->load->view('part/footer-admin');
					break;
					
				case 'edit':
					if ($id) {
						$header = array(
							'header' => 'Edit Artikel',
							'breadcrumbs' => 'artikel',
						);
						$data['article'] = $this->article->get_article($id);
						if(isset($_POST['edit_artikel'])){
							$post_artikel_data = array(
								'judul' => $this->input->post('judul'),
								'kategori' => $this->input->post('kategori'),
								'isi' => $this->input->post('isi'),
								'status' => $this->input->post('status'),
								'waktu_posting' => date('Y-m-d H:i:s'),
							);
							$this->article->edit($id, $post_artikel_data );
							copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
							redirect('dharmaksatrias/artikel');
						}
						$this->load->view('part/header-admin', $header);
						$this->load->view('dharmaksatria/artikel/edit', $data);
						$this->load->view('part/footer-admin');
					} else {
						$this->load->view('dharmaksatria/wrong-room');
					}
					break;
					
				case 'delete':
					$data = $this->dharmaksatria->get('artikel', 'foto', 'id_artikel = '.$id);
					$this->article->delete($id);
					if($data[0]['foto'] != 'default.jpg'){
						unlink('./images/article_images/'.$data[0]['foto']);
					}
					redirect('dharmaksatrias/artikel');
					break;
				
				case 'index':
				default:
					$data = array(
						'header' => 'Data Artikel',
						'breadcrumbs' => 'artikel',
					);
					$this->load->view('part/header-admin', $data);
					$data = array(
						'article' => $this->article->get_article(),
					);
					$this->load->view('dharmaksatria/artikel/index', $data);
					$this->load->view('part/footer-admin');
					break;
			}
		}

		public function foto($page = 'index', $id = null, $type = null){
			$this->validation();
			$this->load->model('photo');
			switch ($page) {
				case 'add':
					$header = array(
						'header' => 'Tambah Foto',
						'breadcrumbs' => 'foto',
					);
					if(isset($_POST['add_artikel'])){
						$post_artikel_data = array(
							'judul' => $this->input->post('judul'),
							'kategori' => $this->input->post('kategori'),
							'isi' => $this->input->post('isi'),
							'status' => $this->input->post('status'),
						);
						$this->article->add($post_artikel_data);
						copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
						redirect('dharmaksatrias/user/index');
					}
					$this->load->view('part/header-admin', $header);
					$this->load->view('dharmaksatria/foto/add');
					$this->load->view('part/footer-admin');
					break;
					
				case 'edit':
					if ($id) {
						$header = array(
							'header' => 'Edit Foto',
							'breadcrumbs' => 'foto',
						);
						$data['article'] = $this->article->get_article($id);
						if(isset($_POST['edit_artikel'])){
							$post_artikel_data = array(
								'judul' => $this->input->post('judul'),
								'kategori' => $this->input->post('kategori'),
								'isi' => $this->input->post('isi'),
								'status' => $this->input->post('status'),
								'waktu_posting' => now(),
							);
							$this->article->edit($id, $post_artikel_data );
							copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
							redirect('dharmaksatrias/artikel');
						}
						$this->load->view('part/header-admin', $header);
						$this->load->view('dharmaksatria/foto/edit', $data);
						$this->load->view('part/footer-admin');
					} else {
						$this->load->view('dharmaksatria/wrong-room');
					}
					break;
					
				case 'delete':
					$data = $this->dharmaksatria->get('foto', 'foto', 'id_foto = '.$id);
					$this->photo->delete($id);
					if($data[0]['foto'] != "default.jpg"){
						unlink('./images/photos/'.$data[0]['foto']);
					}
					redirect('dharmaksatrias/foto');
					break;
				
				case 'index':
				default:
					$data = array(
						'header' => 'Data Foto',
						'breadcrumbs' => 'foto',
					);
					$this->load->view('part/header-admin', $data);
					$data = array(
						'foto' => $this->photo->get_foto(),
					);
					$this->load->view('dharmaksatria/foto/index', $data);
					$this->load->view('part/footer-admin');
					break;
			}
		}

		public function video($page = 'index', $id = null, $type = null){
			$this->validation();
			$this->load->model('video');
			switch ($page) {
				case 'add':
					$header = array(
						'header' => 'Tambah Video',
						'breadcrumbs' => 'video',
					);
					if(isset($_POST['add_artikel'])){
						$post_artikel_data = array(
							'judul' => $this->input->post('judul'),
							'kategori' => $this->input->post('kategori'),
							'isi' => $this->input->post('isi'),
							'status' => $this->input->post('status'),
						);
						$this->article->add($post_artikel_data);
						copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
						redirect('dharmaksatrias/user/index');
					}
					$this->load->view('part/header-admin', $header);
					$this->load->view('dharmaksatria/video/add');
					$this->load->view('part/footer-admin');
					break;
					
				case 'edit':
					if ($id) {
						$header = array(
							'header' => 'Edit Video',
							'breadcrumbs' => 'video',
						);
						$data['article'] = $this->article->get_article($id);
						if(isset($_POST['edit_artikel'])){
							$post_artikel_data = array(
								'judul' => $this->input->post('judul'),
								'kategori' => $this->input->post('kategori'),
								'isi' => $this->input->post('isi'),
								'status' => $this->input->post('status'),
								'waktu_posting' => now(),
							);
							$this->article->edit($id, $post_artikel_data );
							copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
							redirect('dharmaksatrias/artikel');
						}
						$this->load->view('part/header-admin', $header);
						$this->load->view('dharmaksatria/video/edit', $data);
						$this->load->view('part/footer-admin');
					} else {
						$this->load->view('dharmaksatria/wrong-room');
					}
					break;
					
				case 'delete':
					$data = $this->dharmaksatria->get('video', 'foto, url', 'id_video = '.$id);
					$this->video->delete($id);
					if($data[0]['foto'] != "default.jpg"){
						unlink('./images/video_images/'.$data[0]['foto']);
					}
					redirect('dharmaksatrias/foto');
					break;
				
				case 'index':
				default:
					$data = array(
						'header' => 'Data Video',
						'breadcrumbs' => 'video',
					);
					$this->load->view('part/header-admin', $data);
					$data = array(
						'video' => $this->video->get_video(),
					);
					$this->load->view('dharmaksatria/video/index', $data);
					$this->load->view('part/footer-admin');
					break;
			}
		}

		public function slider($page = 'index', $id = null, $type = null){
			$this->validation();
			$this->load->model('slider');
			switch ($page) {
				case 'edit':
					if ($id) {
						$header = array(
							'header' => 'Edit Slider',
							'breadcrumbs' => 'slider',
						);
						$data['article'] = $this->article->get_article($id);
						if(isset($_POST['edit_artikel'])){
							$post_artikel_data = array(
								'judul' => $this->input->post('judul'),
								'kategori' => $this->input->post('kategori'),
								'isi' => $this->input->post('isi'),
								'status' => $this->input->post('status'),
								'waktu_posting' => now(),
							);
							$this->article->edit($id, $post_artikel_data );
							copy("./images/article_images/default.jpg", "./images/article_images/" . $id . ".jpg");
							redirect('dharmaksatrias/artikel');
						}
						$this->load->view('part/header-admin', $header);
						$this->load->view('dharmaksatria/slider/edit', $data);
						$this->load->view('part/footer-admin');
					} else {
						$this->load->view('dharmaksatria/wrong-room');
					}
					break;
					
				case 'delete':
					$data = $this->dharmaksatria->get('slider', 'foto', 'id_slider = '.$id);
					$this->photo->delete($id);
					if($data[0]['foto'] != "default.jpg"){
						unlink('./images/slider/'.$data[0]['foto']);
					}
					redirect('dharmaksatrias/foto');
					break;
				
				case 'index':
				default:
					$data = array(
						'header' => 'Data Slider',
						'breadcrumbs' => 'slider',
					);
					$this->load->view('part/header-admin', $data);
					$data = array(
						'slider' => $this->slider->get_slider(),
					);
					$this->load->view('dharmaksatria/slider/index', $data);
					$this->load->view('part/footer-admin');
					break;
			}
		}
		
	}
?>